package look4ss.aplicativo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
import com.google.firebase.auth.FirebaseAuthWeakPasswordException;
import com.sdsmdg.tastytoast.TastyToast;

public class ActivityRegistro extends AppCompatActivity {
    private EditText enderecoEmaill, Senhaa;
    private Button botaoRegistrar;
    private ProgressBar barraDeLoading;
    private TextView cancelar;
    private FirebaseAuth firebaseAutenticacao;
    private SharedPreferences sharp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registro);

        enderecoEmaill = findViewById(R.id.endereco_de_email);
        Senhaa = findViewById(R.id.senha_login);
        botaoRegistrar = findViewById(R.id.registrar);
        botaoRegistrar.setTextColor(ContextCompat.getColor(ActivityRegistro.this, R.color.textoBotoes));
        cancelar = findViewById(R.id.cancelar);
        barraDeLoading = findViewById(R.id.barraLoading);
        final TextInputLayout txtEmail = findViewById(R.id.layout_entrada_texto);
        final TextInputLayout txtSenha = findViewById(R.id.layout_entrada_texto2);

        txtEmail.setHintEnabled(true);
        txtSenha.setHintEnabled(true);

        firebaseAutenticacao = FirebaseAuth.getInstance();

        cancelar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        botaoRegistrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = enderecoEmaill.getText().toString();
                String senha = Senhaa.getText().toString();

                sharp = getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor edtr = sharp.edit();
                edtr.putString("email", email);
                edtr.putString("senha", senha);
                edtr.apply();

                if (TextUtils.isEmpty(email)) {
                    txtEmail.setError(getString(R.string.digite_email));
                    return;
                } else {
                    txtEmail.setError(null);
                } if (TextUtils.isEmpty(senha)) {
                    txtSenha.setError(getString(R.string.digite_senha));
                    return;
                } else {
                    txtSenha.setError(null);
                }

                barraDeLoading.setVisibility(View.VISIBLE);
                firebaseAutenticacao.createUserWithEmailAndPassword(email, senha).addOnCompleteListener(ActivityRegistro.this, new OnCompleteListener<com.google.firebase.auth.AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<com.google.firebase.auth.AuthResult> task) {

                        barraDeLoading.setVisibility(View.GONE);

                        if(task.getException() instanceof FirebaseAuthUserCollisionException) {
                            TastyToast.makeText(getApplicationContext(), getString(R.string.email_ja_usado), Toast.LENGTH_SHORT, TastyToast.ERROR);
                            botaoRegistrar.setError("Erro");
                            return;
                        }
                        if(task.getException() instanceof FirebaseAuthWeakPasswordException) {
                            TastyToast.makeText(getApplicationContext(), getString(R.string.senha_curta), Toast.LENGTH_SHORT, TastyToast.ERROR);
                            botaoRegistrar.setError("Erro");
                            return;
                        }
                        if(task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            TastyToast.makeText(getApplicationContext(), getString(R.string.erro), Toast.LENGTH_SHORT, TastyToast.ERROR);
                            botaoRegistrar.setError(getString(R.string.erro));
                            return;
                        }
                        if (!task.isSuccessful()) {
                            botaoRegistrar.setError(getString(R.string.erro));
                            TastyToast.makeText(getApplicationContext(), getString(R.string.erro), Toast.LENGTH_SHORT, TastyToast.ERROR);
                        } else {
                          startActivity(new Intent(ActivityRegistro.this, ActivityRegistro2.class));
                            finish();
                        }
                    }
                });
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        barraDeLoading.setVisibility(View.GONE);
    }
}